package com.example.service.impl;

import com.example.common.Page;
import com.example.common.PageQueryParam;
import com.example.dao.TListAndSongMapper;
import com.example.entity.TListAndSongEntity;
import com.example.entity.TSongEntity;
import com.example.service.TListAndSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("tListAndSongService")
public class TListAndSongServiceImpl implements TListAndSongService {

    @Autowired
    private TListAndSongMapper tListAndSongMapper;

    @Override
    public Integer create(TListAndSongEntity tListAndSongEntity) {
        Integer count = tListAndSongMapper.count(tListAndSongEntity);
        if(count>0){
            throw new RuntimeException("歌曲已经存在于歌单中");
        }
        tListAndSongMapper.create(tListAndSongEntity);
        return 1;
    }

    @Override
    public Page<TSongEntity> query(PageQueryParam<TListAndSongEntity> pageQueryParam) {
        List<TSongEntity> rows = tListAndSongMapper.query(pageQueryParam);
        Integer total = tListAndSongMapper.count(pageQueryParam.getParam());
        Page<TSongEntity> page = new Page<>();
        page.setRows(rows);
        page.setTotal(total);
        return page;
    }

    @Override
    public Integer delete(TListAndSongEntity tListAndSongEntity) {
        return tListAndSongMapper.delete(tListAndSongEntity.getIdSong());
    }
}